<?php
// +----------------------------------------------------------------------
// | Description: 1
// +----------------------------------------------------------------------
// | Author: orchief
// +----------------------------------------------------------------------
// | Date: 2018-01-24 16:46:34
// +----------------------------------------------------------------------

namespace app\client\model;

use app\common\model\CommonClient;
use think\Db;

class SkuItem extends CommonClient
{
    protected $fuzzyConditions = null;
    protected $clearConditions = ['SkuId'];
    protected $returnFields = '';
    protected $rangeConditions = null;
    protected $union_fuzzyConditions = null;
    protected $foreignConditions = null;
    protected $name = 'shop_goods_sku_items';

    public function getSku($param)
    {
        // $param['SkuItems'] = [
        //     [1, 1],
        //     [2, 3]
        // ];
        // p($param);
        if(!is_array($param['SkuItems']) or count($param['SkuItems']) <= 0){
            $this -> error = 'SkuItems不能为空';
            return;
        }
        $SkuItems = $param['SkuItems'];
        
        $count = count($SkuItems);
        $conArr = [];
        foreach($SkuItems as $k => $v){
            $conArr[] = "(AttributeId = ".$v[0]." AND ValueId = ".$v[1].")";
        }
        
        $con = implode(' OR ', $conArr);
        $field = "SkuId,count(*) as count,shop_goods_sku.Stock, shop_goods_sku.ProductId,shop_goods_sku.SalePrice, shop_goods_sku.Weight,shop_goods_sku.SkuImg,shop_goods_sku.Content";
        $str = "SELECT $field FROM shop_goods_sku_items LEFT JOIN shop_goods_sku ON shop_goods_sku_items.SkuId = shop_goods_sku.id WHERE {$con} group by SkuId having count = {$count}";
        // p($str);
        try{
            $res = Db::query($str);
            // p($res);
        }catch(\Exception $e)
        {
            $this -> error = '查询错误';
            return false;
        }
        // p($res);
        unset($res[0]['count']);
        return $res[0];
    }
}
